package com.yuanrui.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.base.Preconditions;
import com.yuanrui.circle.api.common.Result;
import com.yuanrui.circle.api.req.RemoveShareCircleReq;
import com.yuanrui.circle.api.req.SaveShareCircleReq;
import com.yuanrui.circle.api.req.UpdateShareCircleReq;
import com.yuanrui.circle.api.vo.ShareCircleVO;
import com.yuanrui.circle.server.entity.ShareCircle;
import com.yuanrui.circle.server.service.ShareCircleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/circle/share/circle/")
public class CircleController {

    @Resource
    private ShareCircleService shareCircleService;

    @RequestMapping("save")
    public Result<Boolean> save(@RequestBody SaveShareCircleReq req) {
        try {
            if (log.isDebugEnabled()) {
                log.info("新增圈子:{}", JSON.toJSONString(req));
            }
            Preconditions.checkNotNull(req.getCircleName(), "圈子名称不能为空");
            Preconditions.checkNotNull(req.getIcon(), "圈子图标不能为空");
            if (req.getParentId() != -1) {
                ShareCircle shareCircle = shareCircleService.getById(req.getParentId());
                Preconditions.checkNotNull(shareCircle, "父圈子不存在");
            }
            Boolean result = shareCircleService.saveCircle(req);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("新增圈子异常", e);
            return Result.fail(false);
        }
    }

    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody UpdateShareCircleReq req) {
        try {
            if (log.isDebugEnabled()) {
                log.info("更新圈子:{}", JSON.toJSONString(req));
            }
            Preconditions.checkNotNull(req.getId(), "圈子ID不能为空");
            Preconditions.checkNotNull(req.getCircleName(), "圈子名称不能为空");
            Preconditions.checkNotNull(req.getIcon(), "圈子图标不能为空");
            if (req.getParentId() != -1) {
                ShareCircle shareCircle = shareCircleService.getById(req.getParentId());
                Preconditions.checkNotNull(shareCircle, "父圈子不存在");
            }
            Boolean result = shareCircleService.updateCircle(req);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("更新圈子异常", e);
            return Result.fail(false);
        }
    }

    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody RemoveShareCircleReq req) {
        try {
            if (log.isDebugEnabled()) {
                log.info("删除圈子:{}", req);
            }
            Preconditions.checkNotNull(req.getId(), "圈子ID不能为空");
            Boolean result = shareCircleService.removeCircle(req);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("删除圈子异常", e);
            return Result.fail(false);
        }
    }

    @RequestMapping("list")
    public Result<Boolean> list() {
        try {
            List<ShareCircleVO> shareCircleVOS = shareCircleService.listResult();
            if (log.isDebugEnabled()) {
                log.info("获取圈子列表:{}", JSON.toJSONString(shareCircleVOS));
            }
            return Result.ok(shareCircleVOS);
        } catch (Exception e) {
            log.error("获取圈子列表异常", e);
            return Result.fail(false);
        }
    }
}
